package com.vinoxm.rss.controller;

import com.vinoxm.common.base.result.BaseResult;
import com.vinoxm.common.core.controller.BaseController;
import com.vinoxm.rss.handler.interceptor.annotation.AwaitRssUpdate;
import com.vinoxm.rss.handler.interceptor.annotation.NeedSecret;
import com.vinoxm.rss.handler.validate.annotation.ValidateParams;
import com.vinoxm.rss.model.RssSubscribe;
import com.vinoxm.rss.service.RssService;
import com.vinoxm.rss.vo.PagerVo;
import com.vinoxm.rss.vo.RssResultVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Log4j2
@NeedSecret
@AwaitRssUpdate
public class RssController extends BaseController<RssService> {

    @PostMapping("addOne")
    public BaseResult addOne(@RequestBody RssSubscribe json) {
        return baseService.addOneRssSubs(json);
    }

    @PostMapping("addMany")
    @NeedSecret("secret-mAou-582014")
    public BaseResult addMany(@RequestBody RssSubscribe[] array) {
        return baseService.addManyRssSubs(array);
    }

    @PostMapping("replaceMany")
    @NeedSecret("secret-mAou-582014")
    @ValidateParams(RssSubscribe.class)
    public BaseResult replaceMany(@RequestBody RssSubscribe[] array) {
        return baseService.replaceManyRssSubs(array);
    }

    @PostMapping("delOne")
    public BaseResult delOne(@RequestBody RssSubscribe json) {
        return baseService.delOneRssSubs(json);
    }

    @PostMapping("delOne.results")
    public BaseResult delOneResults(@RequestBody RssSubscribe json) {
        return baseService.delOneRssSubsResultsById(json);
    }

    @PostMapping("delOne.result")
    public BaseResult delOneResult(@RequestBody RssResultVo json) {
        return baseService.delOneResultById(json, false);
    }

    @PostMapping("hideOne.result")
    public BaseResult hideOneResult(@RequestBody RssResultVo json) {
        return baseService.delOneResultById(json, true);
    }

    @PostMapping("delMany")
    public BaseResult delMany(@RequestBody RssSubscribe[] array) {
        return baseService.delManyRssSubs(array);
    }

    @GetMapping("getOne.results")
    public BaseResult selOne(Long id,@RequestParam(defaultValue = "1") Integer withOutHide) {
        return baseService.selOneRssSubs(id, withOutHide);
    }

    @GetMapping("getOne.detail")
    public BaseResult selOneDetail(Long id) {
        return baseService.getOneDetail(id);
    }

    @PostMapping("getSearch.page")
    public BaseResult selSearchWithPage(@RequestBody PagerVo<RssSubscribe> pager, @RequestParam HashMap<String, Object> params) {
//        return baseService.selSearchRssSubsWithPage(pager, params);
        return BaseResult.Failed();
    }

    @GetMapping("getSearch")
    public BaseResult selSearch(@RequestParam(required = false) String name, @RequestParam(required = false) String season) {
        return baseService.selSearchRssSubs(name, season);
    }

    @PostMapping("editOne")
    public BaseResult editOne(@RequestBody RssSubscribe rssSubscribe) {
        return baseService.editOneRssSubsById(rssSubscribe);
    }

    @PostMapping("setFin")
    public BaseResult setRssSubsFin(@RequestBody RssSubscribe rssSubscribe) {
        return baseService.setSubscribeFin(rssSubscribe);
    }

    @PostMapping("addOne.result")
    public BaseResult addOneResult(@RequestBody RssResultVo result) {
        return baseService.addOneRssResult(result);
    }

    @PostMapping("editOne.result")
    public BaseResult editOneResult(@RequestBody RssResultVo result) {
        return baseService.editOneRssResult(result);
    }

    @PostMapping("updateOne")
    public BaseResult update(@RequestBody Long id) {
        return baseService.updateSubscribe(id);
    }

    @PostMapping("updateMany")
    public BaseResult updateMany(@RequestBody Long[] ids) {
        List<RssSubscribe> list = baseService.getManyRssSubscribeForUpdate(ids);
        return baseService.updateManySubscribe(list);
    }

    @GetMapping("getSeason")
    @AwaitRssUpdate(value = false)
    public BaseResult selSeason() {
        return baseService.selSeason();
    }

    @PostMapping("subsTest")
    @AwaitRssUpdate(value = false)
    public BaseResult rssTest(@RequestBody RssSubscribe rss) {
        return baseService.subscribeRssTest(rss);
    }

    @GetMapping("getSearch.goon")
    @AwaitRssUpdate(value = false)
    public BaseResult getSeasonGoon(String season) {
        return baseService.selSubscribeWithGoon(season);
    }

    @GetMapping("getMatchers")
    @AwaitRssUpdate(value = false)
    public BaseResult getSubscribeMatchers() {
        return baseService.getSubscribeMatchers();
    }
}
